package cn.zyq.ykb.service.task.impl;

import cn.zyq.ykb.beans.config.ConfigTaskType;
import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.beans.task.*;
import cn.zyq.ykb.beans.task.vo.GetMyTaskNumberVo;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.config.ConfigTaskTypeMapper;
import cn.zyq.ykb.mapper.sys.SysAdminMapper;
import cn.zyq.ykb.mapper.task.*;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.service.task.MyTaskAuditService;
import cn.zyq.ykb.service.task.TaskService;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.service.task.MyTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 我的任务领取Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-22
 */
@Service
public class MyTaskServiceImpl  extends BaseService implements MyTaskService {

    @Autowired
    private MyTaskMapper myTaskMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private ConfigTaskTypeMapper configTaskTypeMapper;
    @Autowired
    private SysAdminMapper sysAdminMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MyTaskAuditMapper myTaskAuditMapper;
    @Autowired
    private TaskPromptMapper taskPromptMapper;
    @Autowired
    private TaskStepMapper taskStepMapper;
    @Autowired
    private TaskService taskService;

    /**
     * 查询我的任务领取
     *
     * @param myTaskID 我的任务领取主键
     * @return 我的任务领取
     */
    @Override
    public MyTask selectMyTaskByMyTaskID(Integer myTaskID) {
        MyTask myTask = myTaskMapper.selectByPrimaryKey(myTaskID);
        if (myTask.getDeleted() == 1) {
            return null;
        }
        return myTask;
    }

    /**
     * 查询我的任务领取
     *
     * @param serviceID 业务ID
     * @return 我的任务领取
     */
    @Override
    public MyTask selectMyTaskByServiceID(String serviceID) {
        MyTask param = new MyTask();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.myTaskMapper.selectOne(param);
    }

    /**
     * 查询我的任务领取列表
     *
     * @param pageParam
     * @return 我的任务领取
     */
    @Override
    public PageInfo<MyTask> selectMyTaskList(PageParam pageParam, User loginUser) {
        User user = this.userMapper.selectByPrimaryKey(loginUser.getUserID());
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        pageParam.getKv().put("userID", loginUser.getUserID());
        List<MyTask> list = this.myTaskMapper.list(pageParam.getKv());
        for (MyTask myTask : list) {
            Task task = this.taskMapper.selectByPrimaryKey(myTask.getTaskID());
            this.taskService.checkPriceByUser(task,user);
            myTask.setTask(task);
            ConfigTaskType configTaskType = this.configTaskTypeMapper.selectByPrimaryKey(task.getConfigTaskTypeID());
            myTask.setConfigTaskType(configTaskType);
        }
        return new PageInfo<>(list);
    }

    /**
     * 查询我的任务领取列表
     *
     * @param param
     * @return 我的任务领取
     */
    @Override
    public List<MyTask> selectMyTaskList(MyTask param) {
        param.setDeleted(0);
        return this.myTaskMapper.select(param);
    }
    /**
     * 新增我的任务领取
     *
     * @param param 我的任务领取
     * @return 结果
     */
    @Override
    public int insertMyTask(MyTask param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.myTaskMapper.insertSelective(param);
    }

    /**
     * 修改我的任务领取
     *
     * @param param 我的任务领取
     * @return 结果
     */
    @Override
    public int updateMyTask(MyTask param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.myTaskMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除我的任务领取
     *
     * @param myTaskIDs 需要删除的我的任务领取主键
     * @return 结果
     */
    @Override
    public int deleteMyTaskByMyTaskIDs(Integer[] myTaskIDs) {
        int i = 0;
        for (Integer myTaskID : myTaskIDs) {
            i += this.deleteMyTaskByMyTaskID(myTaskID);
        }
        return i;
    }

    /**
     * 删除我的任务领取信息
     *
     * @param myTaskID 我的任务领取主键
     * @return 结果
     */
    @Override
    public int deleteMyTaskByMyTaskID(Integer myTaskID) {
        MyTask myTask = this.selectMyTaskByMyTaskID(myTaskID);
        if (myTask == null) {
            return 0;
        } else {
            MyTask storeInfo = new MyTask();
            storeInfo.setMyTaskID(myTaskID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.myTaskMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public GetMyTaskNumberVo getMyTaskNumber(SysAdmin admin) {
        int total = 0;
        int yTotal = 0;
        int rTotal = 0;
        SysAdmin sysAdmin = this.sysAdminMapper.selectByPrimaryKey(admin.getSysAdminID());
        if (sysAdmin.getUserType().equals("01")){
            User user = new User();
            user.setDeleted(0);
            user.setPhone(sysAdmin.getPhonenumber());
            User param = this.userMapper.selectOne(user);

            total = this.myTaskMapper.getNumber(param.getUserID());
            yTotal = this.myTaskMapper.getYNumber(param.getUserID());
            rTotal = this.myTaskMapper.getRNumber(param.getUserID());
        }else {
            total = this.myTaskMapper.getNumber(null);
            yTotal = this.myTaskMapper.getYNumber(null);
            rTotal = this.myTaskMapper.getRNumber(null);
        }

        GetMyTaskNumberVo getTaskNumberVo = new GetMyTaskNumberVo();
        getTaskNumberVo.setTotal(total);
        getTaskNumberVo.setRTotal(rTotal);
        getTaskNumberVo.setYTotal(yTotal);
        return getTaskNumberVo;
    }

    @Override
    public Task detail(Integer myTaskID, User loginUser) {
        MyTask myTask = this.myTaskMapper.selectByPrimaryKey(myTaskID);
        Task task = this.taskMapper.selectByPrimaryKey(myTask.getTaskID());
        MyTaskAudit param = new MyTaskAudit();
        param.setMyTaskID(myTaskID);
        param.setDeleted(0);
        param.setUserID(loginUser.getUserID());
        MyTaskAudit myTaskAudit = this.myTaskAuditMapper.selectOne(param);
        task.setMyTaskAudit(myTaskAudit);
        User user = this.userMapper.selectByPrimaryKey(loginUser.getUserID());
        task.setUser(user);
        TaskPrompt taskPrompt = new TaskPrompt();
        taskPrompt.setTaskID(task.getTaskID());
        taskPrompt.setDeleted(0);
        List<TaskPrompt> select2 = this.taskPromptMapper.select(taskPrompt);
        task.setTaskPrompts(select2);
        List<TaskStep> select = this.taskStepMapper.selectByTaskID(task.getTaskID());
        task.setTaskStepList(select);
        task.setMyTasks(myTask);
        ConfigTaskType configTaskType = this.configTaskTypeMapper.selectByPrimaryKey(task.getConfigTaskTypeID());
        task.setConfigTaskType(configTaskType);
        return task;
    }
}
